package com.cn.qtms.util;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @Name:StringUtil
 * @Description: 字符串工具类
 * @Author: Neil
 * @Date: 2019-08-21 17:23
 **/
public class StringUtil {

    public static boolean isAllBlank(final CharSequence... css) {
        if (ArrayUtils.isEmpty(css)) {
            return true;
        }
        for (final CharSequence cs : css) {
            if (isNotBlank(cs)) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNotBlank(final CharSequence cs) {
        return !isBlank(cs);
    }

    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 生成6位随机数字验证码
     */
    public static String getVerifyCode() {
        return String.valueOf(new Random().nextInt(899999) + 100000);
    }

    /**
     * 判断是否为空
     */
    public static boolean isEmpty(Object str) {
        return (str == null || "".equals(str));
    }

    /**
     * 判断是否不为空
     */
    public static boolean isNotEmpty(Object str) {
        return !isEmpty(str);
    }

    /**
     * 获取List参数值
     * @param list
     * @return
     */
    public static String getListString(List<String> list) {
        StringBuilder result = new StringBuilder();
        for (String s : list) {
            result.append(s).append(" ");
        }
        return result.toString();
    }

    /**
     * 生成随机券码
     */
    public static String genRandomNum(int length){
        int  maxNum = 36;
        int i;
        int count = 0;
        //'I','O'取消 原因和数字1和数字0相似
        char[] str = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',  'J', 'K',
                'L', 'M', 'N',  'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while(count < length){
            i = Math.abs(r.nextInt(maxNum));
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count ++;
            }
        }
        return pwd.toString();
    }

    /**
     * 数字转指定长度字符串 前面补0
     */
    public static String codeConvert(Integer code, int len){
        StringBuffer strHao = new StringBuffer(code.toString());
        while (strHao.length() < len) {
            strHao.insert(0,"0");
            //"0".aastrHao;
        }
        return strHao.toString();
    }

    /**
     * map转换成 key=value的字符串
     * @param map
     * @return
     */
    public static String mapToString(Map map){
        StringBuffer buffer=new StringBuffer();
        map.keySet().forEach(item->{
            String result=item+"="+map.get(item)+",";
            buffer.append(result);
        });
        return StringUtils.substringBeforeLast(buffer.toString(),",");
    }
}
